﻿using UnityEngine;
using System.Collections;

using UnityEngine.UI;

public class Loading : MonoBehaviour
{
	public GameObject p1 = null;
	public GameObject p2 = null;
	public GameObject p3 = null;


	public bool enableOnStart  = false;

	protected float value = 0; 						// 进度 0-1

	private static Loading instance = null;
	public static Loading GetInstance()
	{
		return instance;
	}

	void Awake()
	{
		instance = this;
	}

	void Start()
	{

		if (enableOnStart)
			StartLoading (0);
		else
			gameObject.SetActive (false);
	}



	public void StartLoading( float v = 0 )
	{
		value = v;
		gameObject.SetActive (true);
		StartCoroutine (OnLoading ());
	}

    public void EndLoading()
    {
        value = 1f;
    }


	// v 	:0-1
	public void SetProgress( float v )
	{
		value = v;
	}

	IEnumerator OnLoading()
	{
		int count = 0;
		while (value < 1f) {
	

			count++;
			if (count <= 10) {
			
				p2.SetActive (false);

			} else if (count <= 20) {
			
				p2.SetActive (true);
				p3.SetActive (false);
			} else if (count <= 30) {
			
				p2.SetActive (true);
				p3.SetActive (true);
			} else {
			
				count = 0;
			}


			yield return new WaitForEndOfFrame ();
		
		}


		// 满了 隐藏
		gameObject.SetActive(false);
	
	}
}

